package api

import (
	"encoding/json"
	"fmt"
	"github.com/EDDYCJY/go-gin-example/models"
	"github.com/EDDYCJY/go-gin-example/pkg/app"
	"github.com/EDDYCJY/go-gin-example/pkg/e"
	"github.com/EDDYCJY/go-gin-example/pkg/util"
	"github.com/gin-gonic/gin"
)

// 查询虚拟机磁盘列表
type DiskListReq struct {
	NodeID string `form:"node_id" binding:"required"`
	TaskID string `form:"task_id" binding:"required"`
}

func Handle_DiskList(c *gin.Context) {
	appG := app.Gin{C: c}

	var req DiskListReq
	if err := c.ShouldBindQuery(&req); err != nil {
		appG.ResponseError(e.ERROR, "请求参数错误:"+err.Error())
		return
	}

	api := fmt.Sprintf("/api/v1/disk/list?node_id=%v&task_id=%v", req.NodeID, req.TaskID)

	accountInfo, ferror := models.GetAccountByUserId(uint64(c.GetInt64("user_id")))
	if ferror.ErrCode != e.SUCCESS {
		appG.ResponseError(ferror.ErrCode, ferror.ErrMsg)
		return
	}
	params1, ferror := util.GenWalletSign(accountInfo.WalletAddr, accountInfo.PrivateKey)
	if ferror.ErrCode != e.SUCCESS {
		appG.ResponseError(ferror.ErrCode, ferror.ErrMsg)
		return
	}
	params, _ := json.Marshal(params1)

	data := util.PostRequest(api, params)
	if data.Errcode != 0 {
		appG.ResponseError(data.Errcode, data.Errmsg)
	} else {
		appG.ResponseSuccess(data.Data)
	}
}

// 数据盘扩容
type DiskExpandReq struct {
	OrderID    uint64 `form:"order_id" binding:"required"`
	NodeID     string `form:"node_id" binding:"required"`
	TaskID     string `form:"task_id" binding:"required"`
	DiskName   string `form:"disk_name" binding:"required"`
	ExpandSize uint32 `form:"expand_size" binding:"required"`
}

func Handle_DiskExpand(c *gin.Context) {
	appG := app.Gin{C: c}

	var req DiskExpandReq
	if err := c.ShouldBindQuery(&req); err != nil {
		appG.ResponseError(e.ERROR, "请求参数错误:"+err.Error())
		return
	}

	api := fmt.Sprintf("/api/v1/disk/expand?node_id=%v&task_id=%v&disk_name=%v&expand_size=%v",
		req.NodeID, req.TaskID, req.DiskName, req.ExpandSize)

	user_account, ferror := models.GetAccountByUserId(uint64(c.GetInt64("user_id")))
	if ferror.ErrCode != e.SUCCESS {
		appG.ResponseError(ferror.ErrCode, ferror.ErrMsg)
		return
	}
	params_wallet_sign, ferror := util.GenWalletSign(user_account.WalletAddr, user_account.PrivateKey)
	if ferror.ErrCode != e.SUCCESS {
		appG.ResponseError(ferror.ErrCode, ferror.ErrMsg)
		return
	}
	params, _ := json.Marshal(params_wallet_sign)

	rent_order, ferror := models.GetRentOrderByID(req.OrderID)
	if rent_order != nil && rent_order.RentType == 1 { //整机
		data := util.PostRequest(api, params)
		if data.Errcode != 0 {
			appG.ResponseError(data.Errcode, data.Errmsg)
		} else {
			appG.ResponseSuccess(data.Data)
		}
	} else {
		appG.ResponseError(e.ERROR, "不能扩容")
	}
}

// 添加数据盘
type DiskAddReq struct {
	OrderID  uint64 `form:"order_id" binding:"required"`
	NodeID   string `form:"node_id" binding:"required"`
	TaskID   string `form:"task_id" binding:"required"`
	DiskSize uint32 `form:"disk_size" binding:"required"`
}

func Handle_DiskAdd(c *gin.Context) {
	appG := app.Gin{C: c}

	var req DiskAddReq
	if err := c.ShouldBindQuery(&req); err != nil {
		appG.ResponseError(e.ERROR, "请求参数错误:"+err.Error())
		return
	}

	api := fmt.Sprintf("/api/v1/disk/add?node_id=%v&task_id=%v&disk_size=%v",
		req.NodeID, req.TaskID, req.DiskSize)

	user_account, ferror := models.GetAccountByUserId(uint64(c.GetInt64("user_id")))
	if ferror.ErrCode != e.SUCCESS {
		appG.ResponseError(ferror.ErrCode, ferror.ErrMsg)
		return
	}
	params_wallet_sign, ferror := util.GenWalletSign(user_account.WalletAddr, user_account.PrivateKey)
	if ferror.ErrCode != e.SUCCESS {
		appG.ResponseError(ferror.ErrCode, ferror.ErrMsg)
		return
	}
	params, _ := json.Marshal(params_wallet_sign)

	rent_order, ferror := models.GetRentOrderByID(req.OrderID)
	if rent_order != nil && rent_order.RentType == 1 { //整机
		data := util.PostRequest(api, params)
		if data.Errcode != 0 {
			appG.ResponseError(data.Errcode, data.Errmsg)
		} else {
			appG.ResponseSuccess(data.Data)
		}
	} else {
		appG.ResponseError(e.ERROR, "不能增加")
	}
}

// 删除数据盘
type DiskDeleteReq struct {
	OrderID  uint64 `form:"order_id" binding:"required"`
	NodeID   string `form:"node_id" binding:"required"`
	TaskID   string `form:"task_id" binding:"required"`
	DiskName string `form:"disk_name" binding:"required"`
}

func Handle_DiskDelete(c *gin.Context) {
	appG := app.Gin{C: c}

	var req DiskDeleteReq
	if err := c.ShouldBindQuery(&req); err != nil {
		appG.ResponseError(e.ERROR, "请求参数错误:"+err.Error())
		return
	}

	api := fmt.Sprintf("/api/v1/disk/delete?node_id=%v&task_id=%v&disk_name=%v",
		req.NodeID, req.TaskID, req.DiskName)

	user_account, ferror := models.GetAccountByUserId(uint64(c.GetInt64("user_id")))
	if ferror.ErrCode != e.SUCCESS {
		appG.ResponseError(ferror.ErrCode, ferror.ErrMsg)
		return
	}
	params_wallet_sign, ferror := util.GenWalletSign(user_account.WalletAddr, user_account.PrivateKey)
	if ferror.ErrCode != e.SUCCESS {
		appG.ResponseError(ferror.ErrCode, ferror.ErrMsg)
		return
	}
	params, _ := json.Marshal(params_wallet_sign)

	rent_order, ferror := models.GetRentOrderByID(req.OrderID)
	if rent_order != nil && rent_order.RentType == 1 { //整机
		data := util.PostRequest(api, params)
		if data.Errcode != 0 {
			appG.ResponseError(data.Errcode, data.Errmsg)
		} else {
			appG.ResponseSuccess(data.Data)
		}
	} else {
		appG.ResponseError(e.ERROR, "不能删除")
	}
}
